package com.lianniu.mgr.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.dao.IProductMapperDao;
import com.lianniu.mgr.product.model.ProductBrandVo;
import com.lianniu.mgr.product.model.ProductVo;
import com.lianniu.mgr.product.service.IProductService;
import com.lianniu.mgr.teaSideshowPic.dao.TeaSideshowPicMapper;
import com.lianniu.mgr.teaSideshowPic.model.TeaSideshowPic;
import com.lianniu.mgr.util.StringUtil;
import com.lianniu.mgr.util.UUIDUtil;

@Service
public class ProductServiceImpl implements IProductService {
	
	private final static Logger logger = Logger.getLogger(ProductServiceImpl.class);

	@Autowired
	IProductMapperDao productMapper;
	@Autowired
	private TeaSideshowPicMapper teaSideshowPicMapper;
	
	/**
	 * @return
	 * @Description:根据商品名称获取分页列表信息
	 * @param product 商品信息
	 * @return BaseResult

	 * @Date:2016年7月5日       14:20:45
	 * @Version 1.0.0
	 */
	@Override
	public BaseResult getProductsByProductNameForPage(ProductVo product,PaginationInfo paginationInfo) {
		logger.info("getProductsByProductNameForPage begin.");
	    if (!StringUtil.isEmpty(product.getProductName())) {
	    	product.setProductName(product.getProductName().replace(" ", ""));
	    }
	    logger.info("getProductsByProductNameForPage productId:" +product.getProductId());
	    logger.info("getProductsByProductNameForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
		product.setPaginationInfo(paginationInfo);
		List<ProductVo> list = productMapper.getProductsByProductNameForPage(product);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("total", paginationInfo.getTotalRecord());
		map.put("curPage", paginationInfo.getCurPage());
		map.put("rows", list);
		logger.info("getProductsByProductNameForPage:  end");  
		return BaseResult.success(map);
	}

	/**
	 * @return
	 * @Description:获取商品信息
	 * @param product 商品信息
	 * @return BaseResult

	 * @Date:2016年7月5日       14:15:19
	 * @Version 1.0.0
	 */
	@Override
	public BaseResult getProductsByrecord(ProductVo product) {
		 logger.info("getProductsByrecord begin");
		    logger.info("getProductsByrecord Id:" +product.getProductId());
		return BaseResult.success(productMapper.getProductsByrecord(product));
	}

	/**
	 * @return
	 * @Description:插入商品记录
	 * @param product 商品信息
	 * @return BaseResult

	 * @Date:2016年7月5日       14:15:19
	 * @Version 1.0.0
	 */
	@Override
	public BaseResult addProduct(ProductVo product) {
		
		logger.info("addProduct begin");
	    logger.info("addProduct: input data check begin");
	    logger.info("addProduct ProductVo:" +JSON.toJSONString(product));
	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(product.getProductName())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	    
	    
	    logger.info("addProduct: input data check end");
		return BaseResult.success(productMapper.addProduct(product));
	}

	/**
	 * @return
	 * @Description:根据商品id更新
	 * @param product 商品信息
	 * @return  BaseResult

	 * @Date:2016年7月5日      14:15:19
	 * @Version 1.0.0
	 */
	@Override
	public BaseResult updateById(ProductVo pVo) {
		logger.info("updateProductById begin");
	    logger.info("updateProductById: update data check begin");
	    logger.info("updateProductById  product:" +JSON.toJSONString(pVo.getProductId()));
	    logger.info("cateId"+pVo.getCateId());
	    //BaseResult baseResult;
	    /**
	    if (StringUtil.isEmpty(product.getProductName())) {
		      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }*/
	    logger.info("updateProductById: update data check end");
		return BaseResult.success(productMapper.updateById(pVo));
	}

	/**
	 * @return
	 * @Description:根据商品id删除
	 * @param product 商品信息
	 * @return BaseResult

	 * @Date:2016年7月5日       14:15:19
	 * @Version 1.0.0
	 */
	@Override
	public BaseResult delProductById(String id) {
		 logger.info("deleteById begin");
		 logger.info("deleteById Id:" +id);
		return BaseResult.success(productMapper.delProductById(id));
	}

	@Override
	public Boolean checkProductName(String productName) {
		logger.info("checkProductName begin");
	    logger.info("checkProductName productName:" +productName.toString());
	    logger.info("checkProductName: input data check begin");   
	  
	    if (StringUtil.isEmpty(productName)) {       
	      return false;
	    }
	    logger.info("checkProductName: input data check end");
	    
	    ProductVo product=new ProductVo();	   
	    product.setProductName(productName);
	    int count=productMapper.count(product);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

	@Override
	public BaseResult deleteProductBatch(String... id) {
		logger.info("deleteProductBatch begin");
	    logger.info("deleteProductBatch Ids:" +id.toString());
	    logger.info("deleteProductBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteProductBatch: input data check end");
	    
	    return BaseResult.success(productMapper.deleteProductBatch(id));
	}

	@Override
	public BaseResult selectByProductId(ProductVo pvo) {
	    logger.info("selectByProductId begin");
	    logger.info("selectByProductId Id:" +pvo.getProductId());
	  
	    return BaseResult.success(productMapper.selectByProductId(pvo.getProductId()));
	}

	@Override
	public BaseResult selectByLastId() {
		return BaseResult.success(productMapper.selecctByLastId());
	}

	@Override
	public BaseResult getWeixinProductsByProductNameForPage(ProductVo product,
			PaginationInfo paginationInfo) {
		logger.info("getProductsByProductNameForPage begin.");
	    if (!StringUtil.isEmpty(product.getProductName())) {
	    	product.setProductName(product.getProductName().replace(" ", ""));
	    }
	    logger.info("getProductsByProductNameForPage productId:" +product.getProductId());
	    logger.info("getProductsByProductNameForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
		product.setPaginationInfo(paginationInfo);
		List<ProductVo> list = productMapper.getWeixinProductsByProductNameForPage(product);
		if(!CollectionUtils.isEmpty(list)){
			for (ProductVo productVo : list) {
				if ("1".equals(productVo.getCateId())) {
					productVo.setCateName("茶旅联盟");
				}else if ("2".equals(productVo.getCateId())) {
					productVo.setCateName("茶人茶艺(供)");
				}else if ("4".equals(productVo.getCateId())) {
					productVo.setCateName("茶区农产");
				}else if ("5".equals(productVo.getCateId())) {
					productVo.setCateName("茶山估价");
				}else if ("6".equals(productVo.getCateId())) {
					productVo.setCateName("茶山市场");
				}else if ("7".equals(productVo.getCateId())) {
					productVo.setCateName("茶园资讯");
				}else if ("8".equals(productVo.getCateId())) {
					productVo.setCateName("茶人茶艺(需)");
				}else if ("9".equals(productVo.getCateId())) {
					productVo.setCateName("林地转让");
				}
			}
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("total", paginationInfo.getTotalRecord());
		map.put("curPage", paginationInfo.getCurPage());
		map.put("rows", list);
		logger.info("getProductsByProductNameForPage:  end");  
		return BaseResult.success(map);
	}

	@Override
	public BaseResult weixinproductEdit(ProductVo pVo) {
		ProductVo productVo = productMapper.selectByProductId(pVo.getProductId());
		//if ("0".equals(productVo.getStatus())) {
			productVo.setStatus("1");
//		}else if ("1".equals(productVo.getStatus())) {
//			productVo.setStatus("0");
//		}
		productMapper.updateStatusById(productVo);
		return BaseResult.success(productVo);
	}

	@Override
	public BaseResult ProductEditre(ProductVo pVo) {
		ProductVo productVo = productMapper.selectByProductId(pVo.getProductId());
		//if ("0".equals(productVo.getStatus())) {
			productVo.setStatus("2");
//		}else if ("1".equals(productVo.getStatus())) {
//			productVo.setStatus("0");
//		}
			productVo.setReasonMessage(pVo.getReasonMessage());
		productMapper.updateStatusreById(productVo);
		return BaseResult.success(productVo);
	}
	@Override
	public BaseResult ProductEdittg(ProductVo pVo) {
		ProductVo productVo = productMapper.selectByProductId(pVo.getProductId());
		//if ("0".equals(productVo.getStatus())) {
		productVo.setStatus("1");
//		}else if ("1".equals(productVo.getStatus())) {
//			productVo.setStatus("0");
//		}
		productVo.setPriceRetailGuide(pVo.getPriceRetailGuide());
		productMapper.updateStatustgById(productVo);
		return BaseResult.success(productVo);
	}

	@Override
	public BaseResult qxtsp(String productId) {
		ProductVo productVo = new ProductVo();
		productVo.setProductId(productId);
		productVo.setIstsp("0");
		productMapper.updateistspById(productVo);
		List<TeaSideshowPic> teaSideshowPicList = teaSideshowPicMapper.selectByProductId(productId);
		if (!CollectionUtils.isEmpty(teaSideshowPicList)) {
			for (TeaSideshowPic teaSideshowPic : teaSideshowPicList) {
				teaSideshowPicMapper.deleteByPrimaryKey(teaSideshowPic.getId());
			}
		}
		return BaseResult.success();
	}

	@Override
	public BaseResult count() {
		int count = teaSideshowPicMapper.count();
		if (count >= 3 ) {
			return BaseResult.success("no");
		}
		return BaseResult.success("yes");
	}

	@Override
	public BaseResult totsp(ProductVo pVo) {
		pVo.setIstsp("1");
		productMapper.updateistspById(pVo);
		ProductVo productVo = productMapper.selectpicByProductId(pVo.getProductId());
		TeaSideshowPic teaSideshowPic = new TeaSideshowPic();
		teaSideshowPic.setId(UUIDUtil.getUUID());
		teaSideshowPic.setPicPath(productVo.getPicname());
		teaSideshowPic.setProductId(productVo.getProductId());
		teaSideshowPic.setType(productVo.getCateId());
		teaSideshowPicMapper.insertSelective(teaSideshowPic);
		return BaseResult.success();
	}
}
